#include <iostream>
#include <vector>

using namespace std;
// 59.螺旋矩阵II
// 给你一个正整数 n ，生成一个包含 1 到 n^2 所有元素，且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。
class Solution {
private:
    struct Point {
        int row;
        int column;
    };
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> res(n, vector<int>(n,0));
        Point leftTop= {0,0};
        Point rightTop = {0,n-1};
        Point bottomRight = {n-1,n-1};
        Point bottomLeft = {n-1,0};
        int k = 0;  // 当前数字
        while(true) {
            // 上面第一行
            for(int j = leftTop.column; j <= rightTop.column; j++) {
                res[leftTop.row][j] = ++k;
            }
            leftTop.row = leftTop.row + 1;
            rightTop.row = rightTop.row + 1;
            if(rightTop.row > bottomRight.row)
                break;
            // 右边第一列
            for(int i = rightTop.row; i <= bottomRight.row; i++) {
                res[i][rightTop.column] = ++k;
            }
            bottomRight.column = bottomRight.column - 1;
            if(bottomRight.column < bottomLeft.column)
                break;
            // 下面最后一行
            for(int j = bottomRight.column; j >= bottomLeft.column; --j) {
                res[bottomRight.row][j] = ++k;
            }
            bottomLeft.row = bottomLeft.row - 1;
            if(bottomLeft.row < leftTop.row)
                break;
            // 右边第一列
            for(int i = bottomLeft.row; i >= leftTop.row; i--) {
                res[i][bottomLeft.column] = ++k;
            }
//            bottomLeft.row = bottomLeft.row - 1;
//            if(bottomLeft.row > leftTop.row)
//                break;
        }
    }
};
int main() {
    vector<vector<int>> res;
    int n = 3;
    res = Solution().generateMatrix(n);
    return 0;
}
